$(document).ready(function() {
    $.ajax({
        type:"get",
        url:path+"/clazz/getAllClazz",
        dataType:"json",
        success :function (data) {
            var sClazz=$("#selectclazz");
            for(var i in data){
                if(i==0){
                    sClazz.append("<option value="+data[i].id+" selected='selected'>"+data[i].name+"</option>");
                }
                else{
                    sClazz.append("<option value="+data[i].id+">"+data[i].name+"</option>");
                }
            }
        },
        error:function (err) {
            console.log(err)
        }
    })
    $("#confirmPassword").blur(function() {
        var password=$("#password").val();
        var confirmPassword=$("#confirmPassword").val();
        var div=$("#divPassword");
        if(password!=confirmPassword){
            div.show();
        }
        if(password==confirmPassword){
            div.hide();
        }
    })
    $("#email").blur(function() {
        var email=$("#email").val();
        var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;//查看邮箱格式是否正确
        var div=$("#divEmail");
        if(!myReg.test(email)){
            div.show();
        }
        if(myReg.test(email)){
            div.hide();
        }

    })
    $("#b_checkUser").click(function () {
        var name=$("#username").val();
        var password=$("#password").val();
        var email=$("#email").val();
        var type="student";
        var clazzId=parseInt($("#selectclazz").val());
        var confirmPassword=$("#confirmPassword").val();
        var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
        if(name==null||name==""||password==null||password==""||confirmPassword==null||confirmPassword==""||
            email==null||email==""){
            alert("你还有东西没有填写完毕！");
            return;
        }
        if(password!=confirmPassword){
            $("#confirmPassword").focus();
            return;
        }
        if(!myReg.test(email)){
            $("#email").focus();
            return;
        }
        var user={
            name:name,
            password:password,
            email:email,
            type:type,
            clazzId:clazzId
        };
        $.ajax({
            type:"post",
            url:path+"/user/addUser",
            contentType: "application/json; charset=utf-8",
            data:JSON.stringify(user),
            dataType:"text",
            success :function (data) {
                if("exist"==data){
                    alert("用户已存在");
                    window.location.href = path+"/register";
                }
                if("success"==data){
                    alert("注册成功！");
                    var newEmail=$("#email").val();
                    // $.ajax({
                    //
                    // });
                    window.location.href = path+"/login";
                }
            },
            error:function (err) {
                console.log(err)
            }
        })

    })
})









